package controllers

import (
	"github.com/astaxie/beego"
	"wego/models"
	"wego/utils"
)

type BaseController struct {
	beego.Controller
	controllerName string //当前控制名称
	actionName     string //当前action名称
	token          string
}

//初始化
func (this *BaseController) Prepare() {
	this.controllerName, this.actionName = this.GetControllerAndAction()
	if this.actionName != "Login" && this.actionName != "DoUpload" {
		this.token = this.Ctx.Input.Header("Authorization")
		if this.token == "" {
			result := &models.JsonResult{-996, "未登陆", ""}
			this.Data["json"] = result
			this.ServeJSON()
			this.StopRun()
		} else {
			info := make(map[string]interface{})
			err := utils.GetCache(this.token, &info)
			if err != nil {
				result := &models.JsonResult{-996, "token过期", ""}
				this.Data["json"] = result
				this.ServeJSON()
				this.StopRun()
			}
		}
	}
}

func (this *BaseController) JsonResult(code int, msg string, data interface{}) {
	result := &models.JsonResult{code, msg, data}
	this.Data["json"] = result
	this.ServeJSON()
	this.StopRun()
}
